/***************************************************************************
 *   Copyright (C) 2009 by Rysiek Krol,,,   				   *
 *   rysiek.krol@gmail.com   						   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/


#include "imageproperties.h"
#include "rexifdata.h"

#include <QStringList>
#include <QFileInfo>
#include <QResizeEvent>


ImageProperties::ImageProperties(QPixmap &icon, QFileInfo &fileInfo, QWidget* parent, Qt::WFlags fl)
: QDialog( parent, fl ), Ui::ImagePropertiesDialog()
{
    setupUi(this);
    this->propertiesTable->setColumnWidth(0, 150);
    this->propertiesTable->setColumnWidth(1, 300);
    this->setWindowTitle(tr("Property of: ")+fileInfo.absoluteFilePath());
    this->imageLabel->setPixmap(icon);
    this->fillFileProperties(fileInfo);
    this->exifTree->setColumnWidth(0,200);
    this->fillExifData(fileInfo);
}

ImageProperties::~ImageProperties()
{
}

/*$SPECIALIZATION$*/
void ImageProperties::reject()
{
  QDialog::reject();
}

void ImageProperties::accept()
{
  QDialog::accept();
}

/*
* Fills window with File information
*/
void ImageProperties::fillFileProperties(QFileInfo &fileInfo)
{
    //Sets File name
    QTableWidgetItem *newItem = new QTableWidgetItem(tr("File Name: "));
    QImage *image = new QImage(fileInfo.absoluteFilePath());
    QFont font;
    font.setBold(true);
    newItem->setFont(font);
    this->propertiesTable->setItem(0,0,newItem);
    newItem = new QTableWidgetItem(fileInfo.fileName());
    font.setBold(false);
    newItem->setFont(font);
    this->propertiesTable->setItem(0,1,newItem);

    //Sets Image resolution
    newItem = new QTableWidgetItem(tr("Image Resolution: "));
    font.setBold(true);
    newItem->setFont(font);
    this->propertiesTable->setItem(1,0,newItem);
    newItem = new QTableWidgetItem(QString::number(image->height())+"x"+QString::number(image->width()));
    font.setBold(false);
    newItem->setFont(font);
    this->propertiesTable->setItem(1,1,newItem);

    //Sets Image resolution
    newItem = new QTableWidgetItem(tr("Image Size: "));
    font.setBold(true);
    newItem->setFont(font);
    this->propertiesTable->setItem(2,0,newItem);
    double size = fileInfo.size();
    if(size<1024){
        newItem = new QTableWidgetItem(QString::number(size)+ tr(" Bytes"));
    } else if(size<(1024*1024)){
        newItem = new QTableWidgetItem(QString::number(size/1024)+ " kB");
    } else {
        newItem = new QTableWidgetItem(QString::number(size/(1024*1024))+" MB");
    }
    font.setBold(false);
    newItem->setFont(font);
    this->propertiesTable->setItem(2,1,newItem);

    //Sets Image path
    newItem = new QTableWidgetItem(tr("Image Path: "));
    font.setBold(true);
    newItem->setFont(font);
    this->propertiesTable->setItem(3,0,newItem);
    newItem = new QTableWidgetItem(fileInfo.absolutePath());
    font.setBold(false);
    newItem->setFont(font);
    this->propertiesTable->setItem(3,1,newItem);

    delete image;
}

/*
* Fills windows with EXIF information
*/
void ImageProperties::fillExifData(QFileInfo &fileInfo)
{
    QStringList *headers = new QStringList();
    *headers << tr("Label") << tr("Value");
    this->exifTree->setHeaderLabels(*headers);
    RExifData *exifData = new RExifData(fileInfo.absoluteFilePath().toUtf8().data());

    if (exifData->isExif()){
        QList<QTreeWidgetItem *> * items = exifData->getExifTree();
        this->exifTree->addTopLevelItems(*items);
        delete items;
    }
    delete exifData;
    delete headers;
}

/*
* Handles resize event
*/
void ImageProperties::resizeEvent ( QResizeEvent * event ){
    this->propertiesTable->setColumnWidth(1, (event->size().width()-200));
}


